//
//  SectionView.swift
//  DocScaner
//
//  Created by liyahao on 2025/8/11.
//

import SwiftUI

struct SectionView<Data, ID, Content>: View where Content: View, ID: Hashable, Data: RandomAccessCollection {
    var title: String
    var axis: Axis.Set
    var spacing: CGFloat
    let datas: Array<Data.Element>
    let dataId: KeyPath<Data.Element, ID>
    let content: (Data.Element) -> Content
    
    var body: some View {
        VStack(alignment: .leading, spacing: 5) {
            Text(title)
                .font(.title2.weight(.medium))
                .foregroundColor(.black)
                .padding(.bottom)
            
            if axis == .horizontal {
                ScrollView(axis, showsIndicators: false) {
                    HStack(spacing: spacing) {
                        ForEach(datas, id: dataId) { content($0) }
                    }
                }
            } else {
                VStack(spacing: spacing) {
                    ForEach(datas, id: dataId) { content($0) }
                }
            }
        }
    }
}

// MARK: - Initializers
extension SectionView where ID == Data.Element.ID, Data.Element: Identifiable {
    init(title: String, axis: Axis.Set = .horizontal, spacing: CGFloat = 10, datas: Data, content: @escaping (Data.Element) -> Content) {
        self.title = title
        self.axis = axis
        self.spacing = spacing
        self.datas = datas.map { $0 }
        self.dataId = \Data.Element.id
        self.content = content
    }
}

